<?php

namespace App\Repository;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalConst;
use App\Consts\GlobalOfficialConst;
use App\Models\SysAnnounce;
use App\Models\SysMessageTemplate;
use App\Models\SysOfficial;
use App\Models\UserAnnounce;
use App\Models\UserMessageCommentReceive;
use App\Models\UserMessageCommentSend;
use App\Models\UserMessageCount;
use App\Models\UserMessageLikeReceive;
use Illuminate\Database\Eloquent\Builder;

/**
 * @package App\Repository
 * @see MessageRepository
 */
class MessageRepository extends BaseRepository
{
    public function createSenderMessage($loadData)
    {
        return UserMessageCommentSend::getModel($loadData['sender_id'])->create($loadData);
    }

    public function createReceiveMessage($loadData)
    {
        return UserMessageCommentReceive::getModel($loadData['receive_id'])->create($loadData);
    }

    /**
     * @param Builder $query
     * @param $searchParams
     * @return Builder
     */
    public function getCommentProvider($query, $searchParams)
    {
        $provider = $query->where(function (Builder $query) use ($searchParams) {
            if (!empty($searchParams['last_id'])) {
                $query->where('id', '<', $searchParams['last_id']);
            }
            if (!empty($searchParams['id'])) {
                $query->where('id', $searchParams['id']);
            }
            if (!empty($searchParams['sender_id'])) {
                $query->where('sender_id', $searchParams['sender_id']);
            }
            if (!empty($searchParams['receive_id'])) {
                $query->where('receive_id', $searchParams['receive_id']);
            }
            $query->where('is_deleted', 0);
        })->orderBy('created_at', 'desc');
        return $this->setPagination($provider);
    }

    /**
     * 我发出的评论
     * @param $searchParams
     * @return Builder
     */
    public function getUserSendCommentList($searchParams)
    {
        $provider = UserMessageCommentSend::getInstance($searchParams['sender_id'])
            ->where('sender_id', $searchParams['sender_id'])
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE);

        if (!empty($searchParams['last_id'])) {
            $time = UserMessageCommentSend::getInstance($searchParams['sender_id'])
                ->select(['created_at'])
                ->where('message_id', $searchParams['last_id'])
                ->value('created_at');
            $provider->where('created_at', '<', $time);
        }

        $provider->orderByDesc('created_at');

        return $this->setPagination($provider);
    }

    /**
     * 我收到的评论
     * @param $searchParams
     * @return Builder
     */
    public function getUserReceiveCommentList($searchParams)
    {
        $provider = UserMessageCommentReceive::getInstance($searchParams['receive_id'])
            ->where('receive_id', $searchParams['receive_id'])
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE);

        if (!empty($searchParams['last_id'])) {
            $time = UserMessageCommentReceive::getInstance($searchParams['receive_id'])
                ->select(['created_at'])
                ->where('message_id', $searchParams['last_id'])
                ->value('created_at');
            $provider->where('created_at', '<', $time);
        }

        $provider->orderByDesc('created_at');

        return $this->setPagination($provider);
    }

    /**
     * 我收到的点赞
     * @param $searchParams
     * @return Builder
     */
    public function getUserReceiveLikeList($searchParams)
    {
        $provider = UserMessageLikeReceive::getInstance($searchParams['receive_id'])
            ->where('receive_id', $searchParams['receive_id'])
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->where('sender_id', '<>', $searchParams['receive_id']);

        if (!empty($searchParams['last_id'])) {
            $time = UserMessageLikeReceive::getInstance($searchParams['receive_id'])
                ->select(['created_at'])
                ->where('message_id', $searchParams['last_id'])
                ->value('created_at');
            $provider->where('created_at', '<', $time);
        }

        $provider->orderByDesc('created_at');

        return $this->setPagination($provider);
    }

    /**
     * 我收到的公告信息
     * @param $searchParams
     * @return Builder
     */
    public function getUserAnnounce($searchParams)
    {
        $officials = SysOfficial::getInstance()
            ->where('status', GlobalOfficialConst::STATUS_NORMAL)
            ->get();
        $announceUserId = ArrayHelper::getColumn($officials, 'user_id');

        $provider = UserAnnounce::getInstance($searchParams['user_id'])
            ->where('user_id', $searchParams['user_id']);


        if ($announceUserId) {
            $provider->whereIn('announce_user_id', (array)$announceUserId);
        }

        if (!empty($searchParams['last_id'])) {
            $provider->where('announce_id', '<', $searchParams['last_id']);
        }

        $provider->orderByDesc('created_at');

        return $this->setPagination($provider, ['not_use_pagination' => true]);
    }

    /**
     * 我收到的系统公告
     * @param $idAry
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getSysAnnounce($idAry)
    {
        $model = new SysAnnounce();
        return $model::getInstance()
            ->select($model->fields['message'])
            ->whereIn('id', $idAry)
            ->get();
    }

    /**
     * 系统公告模板
     * @param $idAry
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getSysMessageTpl($idAry)
    {
        $model = new SysMessageTemplate();
        return $model::getInstance()
            ->select($model->fields['message'])
            ->whereIn('id', $idAry)
            ->get();
    }

    /**
     * 未读消息
     * @param $userId
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getUserUnread($userId)
    {
        return UserMessageCount::getInstance($userId)
            ->where('user_id', $userId)
            ->get();
    }

}
